//
//  BleDevice.m
//  bleDemo
//
//  Created by zt on 2020/4/13.
//  Copyright © 2020 XB. All rights reserved.
//

#import "BleDevice.h"
#import "BleManager.h"


@implementation BleDevice

#pragma mark 对外公开的方法
- (void)connect {
    self.peripheral.delegate = self;
//    [[BleManager manager] connect:self];
}

- (void)disconnect {
    // 发送断开连接的通知
//    [[BleManager manager] disconnect:self];

}

- (void)clearSend {

}

- (void)sendData:(NSData *)data {
    
}

#pragma mark 外设代理方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error {
    PLog(@"发现服务 回调%@", NSStringFromSelector(_cmd));
    // 这里要遍历需要的服务  FFF0
//    [peripheral discoverCharacteristics:nil forService:service];
    // 过滤服务
    if (self.delegate && [self.delegate respondsToSelector:@selector(device:filterServices:)]) {
        [peripheral discoverServices:self.filterServices];
    } else {
        [peripheral discoverServices:nil];
    }
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error {
    // 判断特征是什么特征（可以订阅、可以写入，可以读取的）
    // 可读取的特征，正常比较扫用，通常是用写入通道，写入响应的指令，让蓝牙把数据返回回来
    PLog(@"发现特征%@", NSStringFromSelector(_cmd));
    // 遍历特征
    [service.characteristics enumerateObjectsUsingBlock:^(CBCharacteristic * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    }];
}


- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    PLog(@"收到数据%@", NSStringFromSelector(_cmd));
    // 数据通过代理传出去
    if (self.delegate && [self.delegate respondsToSelector:@selector(device:didUpdateData:)]) {
        [self.delegate device:self didUpdateData:characteristic.value];
    }
}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    PLog(@"订阅状态发生改变%@", NSStringFromSelector(_cmd));
}

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    PLog(@"写入特征值");
    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤%@", NSStringFromSelector(_cmd));
}

//- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error {
//    PLog(@"写入特征值");
//    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤%@", NSStringFromSelector(_cmd));
//}

//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error {
//    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤%@", NSStringFromSelector(_cmd));
//}

//- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral {
//    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤%@", NSStringFromSelector(_cmd));
////    NSLog(@"%@",NSStringFromSelector(_cmd));
//}


//- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices {
//    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤%@", NSStringFromSelector(_cmd));
//}

//- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error {
//    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤10_13, 8_0 %@", NSStringFromSelector(_cmd));
//}

//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
//    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤%@", NSStringFromSelector(_cmd));
//}


//- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error {
//    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤%@", NSStringFromSelector(_cmd));
//}

//- (void)peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral *)peripheral {
//    PLog(@"❤❤❤❤❤❤❤❤❤❤❤❤❤❤%@", NSStringFromSelector(_cmd));
//}

// ios 11 以后有效
//- (void)peripheral:(CBPeripheral *)peripheral didOpenL2CAPChannel:(nullable CBL2CAPChannel *)channel error:(nullable NSError *)error {
//
//}

#pragma setter && getter
- (NSMutableArray *)filterServices {
    if (!_filterServices) {
        _filterServices = [NSMutableArray new];
    }
    return _filterServices;
}

@end
